package com.cmc.cloud.cmclink.doc.controller.admin.manifest;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.api.document.emums.SoDataSourceEnum;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifest.ManifestInfoRespVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifest.ManifestPageReqVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifest.ManifestUpdateInfoReqVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestcntr.ManifestCntrRespVO;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.service.manifest.ManifestService;
import com.cmc.cloud.cmclink.doc.service.so.BizSoService;
import com.cmc.cloud.cmclink.doc.vo.manifestvo.manifest.ManifestCancelVO;
import com.cmc.cloud.cmclink.doc.vo.manifestvo.manifest.ManifestSalveReqVO;
import com.cmc.cloud.cmclink.doc.vo.manifestvo.manifest.ManifestSoPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.manifestvo.manifest.ManifestTransitTermReqVO;
import com.cmc.cloud.cmclink.doc.vo.manifestvo.manifestcntr.ManifestCntrCheckLclReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.sovo.SoRespVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import com.cmes.framework.security.core.util.SecurityFrameworkUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * 舱单主前端接口Controller
 *
 * @author chenzheng
 */
@Tag(name = "管理后台 - 舱单主")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/manifest")
public class ManifestController {
    @Resource
    private BizSoService bizSoService;
    @Resource
    private ManifestService manifestService;

    @Operation(summary = "获得SO可以舱单分页")
    @PostMapping("/getSoManifest")
    @UseResponsePropertyAdvice
    @PreAuthorize("@ss.hasPermission('doc:createManifest:search')")
    public CommonResult<PageResult<SoRespVO>> getSoManifest(@Valid @RequestBody ManifestSoPageReqVO pageVO) {
        return CommonResult.success(bizSoService.getSoManifest(pageVO));
    }

    @Operation(summary = "批量创建舱单主来源SO")
    @PostMapping("/batchCreateMasterFromSo")
    @PreAuthorize("@ss.hasPermission('doc:createManifest:confirm')")
    public CommonResult<List<Long>> batchCreateMasterFromSo(@RequestBody List<Long> soIdList) {
        return CommonResult.success(manifestService.batchCreateMasterFromSo(soIdList, String.valueOf(SecurityFrameworkUtils.getLoginUserId()), SoDataSourceEnum.MANUAL.getValue(),false));
    }

    @Operation(summary = "创建子舱单")
    @PostMapping("/createSlaveManifest")
    @PreAuthorize("@ss.hasPermission({'doc:manifest:createHouse','doc:manifestMulti:createHouse'})")
    public CommonResult<Long> createSlaveManifest(@RequestBody ManifestSalveReqVO salveReq) {
        return CommonResult.success(manifestService.createSlaveManifest(salveReq, String.valueOf(SecurityFrameworkUtils.getLoginUserId()), SoDataSourceEnum.MANUAL.getValue()));
    }


    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping(value = "/importCntr", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "excel导入箱信息")
    @PreAuthorize("@ss.hasPermission({'doc:manifest:importCntr','doc:manifestMulti:importCntr'})")
    public void importCntr(@RequestPart @RequestParam(value = "file") MultipartFile file, HttpServletResponse response) throws Exception {
        manifestService.importCntr(file, String.valueOf(SecurityFrameworkUtils.getLoginUserId()), response);
    }


    @Operation(summary = "批量删除未确认舱单")
    @PostMapping("/batchDelete")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission({'doc:manifest:batchDelete','doc:manifestMulti:batchDelete','doc:manifestMulti:delete'})")
    public CommonResult<Void> batchDelete(@RequestBody List<Long> ids) {
        manifestService.deleteManifestBatch(ids, String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
        return CommonResult.success();
    }

    @Operation(summary = "货物和箱数据校验")
    @PostMapping("/checkCargoCntrData")
    public CommonResult<String> checkCargoCntrData(@Valid @RequestBody ManifestUpdateInfoReqVO updateReqVO) {
        List<String> list = manifestService.checkCargoCntrData(updateReqVO);
        if (CollectionUtil.isNotEmpty(list)) {
            CommonResult.success(StringUtils.join(list, ";"));
        }
        return CommonResult.success(null);
    }

    @Operation(summary = "更新舱单")
    @PostMapping("/updateManifest")
    @PreAuthorize("@ss.hasPermission({'doc:manifest:edit','doc:manifestMulti:edit'})")
    public CommonResult<Long> updateManifest(@Valid @RequestBody ManifestUpdateInfoReqVO updateReqVO) {
        return CommonResult.success(manifestService.updateManifest(updateReqVO, String.valueOf(SecurityFrameworkUtils.getLoginUserId())));
    }

    @Operation(summary = "更新舱单并确认")
    @PostMapping("/updateManifestAndConfirm")
    @PreAuthorize("@ss.hasPermission({'doc:manifest:batchConfirm','doc:manifestMulti:batchConfirm'})")
    public CommonResult<String> updateManifestAndConfirm(@Valid @RequestBody ManifestUpdateInfoReqVO updateReqVO) {
        return CommonResult.success(manifestService.updateManifestAndConfirm(updateReqVO, String.valueOf(SecurityFrameworkUtils.getLoginUserId())));
    }

    @Operation(summary = "未确认舱单批量确认")
    @PostMapping("/batchConfirm")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission({'doc:manifest:batchConfirm','doc:manifestMulti:batchConfirm'})")
    public CommonResult<String> batchConfirm(@RequestBody List<Long> ids) {
        return CommonResult.success(manifestService.batchConfirm(ids, String.valueOf(SecurityFrameworkUtils.getLoginUserId()),true));
    }

    @Operation(summary = "获得舱单详情")
    @GetMapping("/getManifestDetail/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission({'doc:manifest:preview','doc:manifestMulti:preview'})")
    public CommonResult<ManifestInfoRespVO> getManifestDetail(@PathVariable("id") Long id) {
        return CommonResult.success(manifestService.getManifest(id));
    }

    @Operation(summary = "通过bookingNo获得主舱单详情")
    @GetMapping("/getManifestDetailByBookingNo/{bookingNo}")
    @Parameter(name = "bookingNo", description = "bookingNo", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:manifest:preview')")
    public CommonResult<ManifestInfoRespVO> getManifestDetailByBookingNo(@PathVariable("bookingNo") String bookingNo) {
        return CommonResult.success(manifestService.getManifestDetailByBookingNo(bookingNo));
    }


    @Operation(summary = "获得舱单分页")
    @PostMapping("/getManifestPage")
    @PreAuthorize("@ss.hasPermission('doc:manifest:search')")
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<com.cmc.cloud.cmclink.doc.vo.manifestvo.manifest.ManifestRespVO>> getManifestPage(@Valid @RequestBody ManifestPageReqVO pageVO) {
        return CommonResult.success(manifestService.getManifestPage(pageVO));
    }


    @Operation(summary = "检查箱号是否拼箱")
    @PostMapping("/checkManifestLcl")
    public CommonResult<Boolean> checkManifestLcl(@Valid @RequestBody ManifestCntrCheckLclReqVO reqVO) {
        return CommonResult.success(manifestService.checkManifestLcl(reqVO));
    }


    @Operation(summary = "确认舱单批量确认检查lcl")
    @PostMapping("/checkConfirmLcl")
    @PreAuthorize("@ss.hasPermission({'doc:createManifest:search','doc:manifestMulti:search'})")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<String> checkConfirmLcl(@RequestBody List<Long> ids) {
        String check = null;
        String checkConfirm = manifestService.batchConfirm(ids, String.valueOf(SecurityFrameworkUtils.getLoginUserId()), false);
        String checkLcl = manifestService.checkConfirmLcl(ids, String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
        if (checkConfirm != null) {
            check = checkConfirm;
        }
        if (checkLcl != null) {
            if (check == null) {
                check = checkLcl;
            } else {
                check = check + ";" + checkLcl;
            }
        }
        return CommonResult.success(check);
    }

    @Operation(summary = "批量更新中转类型")
    @PostMapping("/updateTransitTerm")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission({'doc:manifest:transitTerm','doc:manifestMulti:transitTerm'})")
    public CommonResult<Void> updateTransitTerm(@Valid @RequestBody ManifestTransitTermReqVO transitTermReq) {
        manifestService.updateTransitTerm(transitTermReq, String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
        return CommonResult.success();
    }

    @Operation(summary = "批量取消舱单检查")
    @PostMapping("/checkCancelBatch")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission({'doc:manifest:batchCancelConfirm','doc:manifestMulti:batchCancelConfirm'})")
    public CommonResult<ManifestCancelVO> checkCancel(@RequestBody List<Long> ids) {
        return CommonResult.success(manifestService.checkCancel(ids, String.valueOf(SecurityFrameworkUtils.getLoginUserId())));
    }

    @Operation(summary = "批量取消舱单")
    @PostMapping("/cancelConfirmBatch")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission({'doc:manifest:batchCancelConfirm','doc:manifest:batchCancelConfirm','doc:manifestMulti:batchCancelConfirm'})")
    public CommonResult<Void> cancelConfirm(@RequestBody List<Long> ids) {
        manifestService.cancelConfirm(ids, String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
        return CommonResult.success();
    }

    @Operation(summary = "通过bookingNo查询主舱单箱信息")
    @GetMapping("/queryManifestCntrListByBookingNo/{bookingNo}")
    @Parameter(name = "bookingNo", description = "bookingNo", required = true, example = "1024")
    public CommonResult<List<ManifestCntrRespVO>> queryManifestCntrListByBookingNo(@PathVariable("bookingNo") String bookingNo) {
        return CommonResult.success(manifestService.getManifestCntrListByBookingNo(bookingNo));
    }



}
